package edu.mercer.medapps.documentbldr;

import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.common.base.Optional;
import com.google.common.reflect.Reflection;


/**
 * Demonstrate use of Document Builder interface.<p> 
 *
 */

public class BldrDemoApp 
{
    public static void main( String[] args )
    {
    	try
    	{
    	
    		for(int ct=0; ct < args.length; ct++) new DemoDocBlder(args[ct]);

    		if( args.length < 1 ) System.out.println( Help.factory()
    													  .consoleUsage() );
    	
    	}
    	catch(final IOException fileErr)
    	{
    		final String errMsg = String.format(
    				"Unable access file, error: %s", fileErr.getMessage() );
    		
    		System.out.println( errMsg );
    		
    		Logger errLog = Logger.getLogger( Reflection.class
    											 	    .getCanonicalName() );
    		
    		errLog.log(Level.WARNING, errMsg, fileErr);
    		
    	}
    	catch(final Throwable err)
    	{
    		err.printStackTrace();
    		
    	}//try
    	
    }//main

    
}//class


class Help
{
	/** command line help message */
	
	private static final String _consoleUsage = "Specify text filename(s)" + 
			" separated by spaces. Example:\n\ttestfile.csv secondfile.txt";
	
	private Help()
	{
		super();
		
	}//constructor

	
	/** @return Help - create Help instance */
	
	static Help factory()
	{ 
		return new Help();
		
	}//method

	
	/** @return String - help message for command line usage */
	
	String consoleUsage()
	{
		return _consoleUsage;
		
	}//method
	
	
}//class


class DemoDocBlder
{
	Optional<Document<DocumentProperties>> _document = Optional.absent();
	
	Optional<String> _filename = Optional.absent();
	
	static Integer _invocations = 0;
	
	/**
	 * Demonstrate Document interface, builder from CSV or other text file.<p>
	 *  
	 * @param fileNameIn - String of filename with any path, not null 
	 * 
	 * @throws IOException   unable access specified text file
	 */
	
	DemoDocBlder(final String fileNameIn) throws IOException
	{
		super();
		
		_invocations++;

		System.out.print(String.format("%nDemonstration #%d for: %s%n%n", 
				_invocations, fileNameIn));

		_filename = Optional.of( fileNameIn );

		showOffDocBuilder();
		
	}//constructor


	/**
	 * Demonstrate Document interface, builder from CSV or other text file.<p>
	 * 
	 * @throws IOException  unable access specified text file
	 * 
	 * @see DemoDocBlder
	 */

	private DemoDocBlder showOffDocBuilder() throws IOException
	{
		_document = Optional.of( DocumentDemo
					.builder()
					.fromFile( new ValidFile(_filename.get()) )
					.name("Test file " + _invocations)
					.build() );
		
		return showOffDocument().showOffAnalyzer();
	
	}//method

	
	private DemoDocBlder showOffDocument() throws IOException
	{
		final DocumentProperties docProp = _document.get()
													.properties();

		final Iterator<String> docPropertiesIter = Document.FEATURES
														   .SET
														   .EXPOSED
														   .iterator();

		StringBuilder outputStrBld = new StringBuilder();
		
		while( docPropertiesIter.hasNext() )
		{
			String propName  = docPropertiesIter.next();
			
			String propValue = docProp.properties().get(propName);
			
			outputStrBld.append( String.format("%s : %s%n", propName, 
															propValue) );
			
		}//while

		System.out.print(outputStrBld);
		
		return this;
		
	}//method
	
	
	private DemoDocBlder showOffAnalyzer() throws IOException
	{
		final DocumentAnalyzer _documentAnalyzer = DocumentAnalyzer.factory(
				_document.get() );
		
		final Iterator<String> techElements = _documentAnalyzer.technical()
															   .list()
															   .keySet()
															   .iterator();
		
		while( techElements.hasNext() )
		{ 
			String techItem = techElements.next();
			
			String outputStr = String.format( "%s : %d", techItem,
					
					_documentAnalyzer.technical()
									 .list()
									 .get(techItem) );
					
			System.out.println(outputStr);
			
		}//while

		final Iterator<String> lexicalElements = _documentAnalyzer.lexical()
				.list()
				.keySet()
				.iterator();

		while( lexicalElements.hasNext() )
		{ 
			String lexItem = lexicalElements.next();

			String outputStr = String.format( "%s : %s", lexItem,

					_documentAnalyzer.lexical()
					.list()
					.get(lexItem) );

			System.out.println(outputStr);

		}//while
		
		return this;
		
	}//method

	
}//class